package com.nf.health.app.healthbank;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.healthbank.adapter.BottomDoctorListAdapter;
import com.nf.health.app.healthbank.bean.HealthBankDetailBean;
import com.nf.health.app.inquiry.bean.DoctorDetailBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ImageLoaderUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/10/8.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description
 * @email he_pann@163.com
 */
public class MyHealthBankDetailActivity extends BaseActivity {

    public static final String DOCTOR_ID = "doctor_id";
    public static final String DOCTOR_DETAIL_TITLE = "health.bank.title";

    private static final String GET_ATTENTION_BANK_BY_DOCTORID = "get_attention_bank_by_doctorid";
    private static final String GET_ATTENTION_BANK_DOCTOR_LIST = "get_attention_bank_doctor_list";
    private static final String GET_ATTENTION_BANK_DOCTOR_LIST_All = "get_attention_bank_doctor_list.query.all";
    private static final String DEL_ATTENTION_BANK = "delAttentionBank";
    private static final String ADD_ATTENTION_BANK = "addAttentionBank";

    @Bind(R.id.iv_head_icon)
    CircleImageView ivHeadIcon;
/*    @Bind(R.id.tv_hospital_name)
    TextView tvHospitalName;*/
   /* @Bind(R.id.tv_doctor_expert_num)
    TextView tvDoctorExpertNum;*/
/*    @Bind(R.id.tv_patient_num)
    TextView tvPatientNum;*/
    @Bind(R.id.tv_follow_state)
    TextView tvFollowState;
//    @Bind(R.id.tv_bank_goodness)
//    TextView tvBankGoodness;
//    @Bind(R.id.tv_bank_introduction)
//    TextView tvBankIntroduction;
    @Bind(R.id.tv_show_more)
    TextView tvShowMore;
    @Bind(R.id.iv_show_more)
    ImageView ivShowMore;
    /*@Bind(R.id.ll_show_more)
    LinearLayout llShowMore;*/
 /*   @Bind(R.id.rl_more_doctor)
    RelativeLayout rlMoreDoctor;*/
/*    @Bind(R.id.rv_doctors)
    RecyclerView rvDoctors;*/
    @Bind(R.id.tv_healthy_prefferor_num)
    TextView tv_healthy_prefferor_num;
    @Bind(R.id.tv_healthy_docotor_num)
    TextView tv_healthy_docotor_num;
    @Bind(R.id.tv_healthy_fun_num)
    TextView tv_healthy_fun_num;

    @Bind(R.id.tv_hostipal_name_type01)
    TextView tv_hostipal_name_type01;
    @Bind(R.id.tv_goodat01)
    TextView tv_goodat01;
   @Bind(R.id.tv_detail01)
    TextView tv_detail01;
    @Bind(R.id.rl_more_doctor01)
    RelativeLayout rl_more_doctor01;
    @Bind(R.id.tv_docotor_name_type01)
    TextView tv_docotor_name_type01;
      @Bind(R.id.tv_look_more01)
    TextView tv_look_more01;
      @Bind(R.id.tv_look_more02)
    TextView tv_look_more02;

    @Bind(R.id.rv_doctors01)
    RecyclerView rv_doctors01;

    @Bind(R.id.tv_hostipal_name_type02)
    TextView tv_hostipal_name_type02;
    @Bind(R.id.tv_goodat02)
    TextView tv_goodat02;
    @Bind(R.id.tv_detail02)
    TextView tv_detail02;
    @Bind(R.id.rl_more_doctor02)
    RelativeLayout rl_more_doctor02;
    @Bind(R.id.tv_docotor_name_type02)
    TextView tv_docotor_name_type02;
    @Bind(R.id.rv_doctors02)
    RecyclerView rv_doctors02;
    @Bind(R.id.ll_doctors01)
    LinearLayout ll_doctors01;
    @Bind(R.id.ll_doctors02)
    LinearLayout ll_doctors02;

    private String doctorId,mTitle;
    private HealthBankDetailBean mBankBean;
    private List<DoctorDetailBean> mDoctorList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_healthy_bank_detail);
        ButterKnife.bind(this);
        doctorId = getIntent().getStringExtra(DOCTOR_ID);
        mTitle = getIntent().getStringExtra(DOCTOR_DETAIL_TITLE);
        initView();
        initListener();
        initData();
    }

    private void initView() {
        if(TextUtils.isEmpty(mTitle)){
            mTitle = "健康银行";
        }
        ViewUtil.setText(this, R.id.titlebar_title_tv, mTitle);
        //默认隐藏两个按钮
        tv_look_more02.setVisibility(View.GONE);
        tv_look_more01.setVisibility(View.GONE);

    }

    private void initListener() {
        tvFollowState.setOnClickListener(this);
        tv_look_more01.setOnClickListener(this);
        tv_look_more02.setOnClickListener(this);
    }

    private void initData() {
        provider.getAttentionBankByDoctorid(doctorId, GET_ATTENTION_BANK_BY_DOCTORID);
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        super.handleActionSuccess(actionName, object);
        if (GET_ATTENTION_BANK_BY_DOCTORID.equals(actionName)) {
            mBankBean = (HealthBankDetailBean) object;
            if (mBankBean != null) {
                initViewByBean();
            }
        } else if (GET_ATTENTION_BANK_DOCTOR_LIST.equals(actionName)) {

            List<DoctorDetailBean> mDoctorList1 = (List<DoctorDetailBean>) object;
            initDoctorList1(mDoctorList1);
        } else if (GET_ATTENTION_BANK_DOCTOR_LIST_All.equals(actionName)) {
            List<DoctorDetailBean> mDoctorList2 = (List<DoctorDetailBean>) object;
                initDoctorList2(mDoctorList2);
        }  else if (DEL_ATTENTION_BANK.equals(actionName)) {
            showToast("取消关注");
            //不再请求接口刷新了
            mBankBean.setIsfollow("n");
            initViewByBean();
        } else if (ADD_ATTENTION_BANK.equals(actionName)) {
            showToast("关注成功");
            mBankBean.setIsfollow("y");
            initViewByBean();
        }
    }

    private void initDoctorList1( List<DoctorDetailBean> mDoctorList) {
        if(mDoctorList == null || mDoctorList.size() == 0){
            tv_look_more01.setVisibility(View.GONE);
            View view = View.inflate(MyHealthBankDetailActivity.this,R.layout.textview_no_data,null);
            TextView textView = (TextView) view.findViewById(R.id.tv_text_nodate);
            textView.setText("暂无推荐的医生");
            textView.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
            ll_doctors01.removeAllViews();
            ll_doctors01.setGravity(Gravity.CENTER);
            ll_doctors01.addView(view);
                return;
        }
        tv_look_more01.setVisibility(View.VISIBLE);
        BottomDoctorListAdapter mAdapter = null;
        if (mAdapter == null) {
            mAdapter = new BottomDoctorListAdapter(mDoctorList, this);
            //设置布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rv_doctors01.setLayoutManager(linearLayoutManager);
            rv_doctors01.setAdapter(mAdapter);
        }else{
            mAdapter.notifyDataSetChanged();
        }
    }
    private void initDoctorList2( List<DoctorDetailBean> mDoctorList) {
        if(mDoctorList == null || mDoctorList.size() == 0){
            tv_look_more02.setVisibility(View.GONE);
            View view = View.inflate(MyHealthBankDetailActivity.this,R.layout.textview_no_data,null);
            TextView textView = (TextView) view.findViewById(R.id.tv_text_nodate);
            textView.setBackgroundColor(ContextCompat.getColor(this,R.color.white));
            textView.setText("暂无推荐的医生");
            ll_doctors02.removeAllViews();
            ll_doctors02.setGravity(Gravity.CENTER);
            ll_doctors02.addView(view);
            return;
        }
        tv_look_more02.setVisibility(View.VISIBLE);
        BottomDoctorListAdapter mAdapter2 = null;
            if (mAdapter2 == null) {
                mAdapter2 = new BottomDoctorListAdapter(mDoctorList, this);
                //设置布局管理器
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
                linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rv_doctors02.setLayoutManager(linearLayoutManager);
                rv_doctors02.setAdapter(mAdapter2);
            }else {
                mAdapter2.notifyDataSetChanged();
            }

        }

    private void initViewByBean() {
        //基本信息
        ImageLoaderUtils.getinstance(this).getImage(ivHeadIcon, mBankBean.getHimg(), R.drawable.default_image);
        tv_healthy_fun_num.setText(mBankBean.getUsernum()+"");
        tv_healthy_prefferor_num.setText(mBankBean.getSnum()+"");
        tv_healthy_docotor_num.setText(mBankBean.getDoctornum()+"");

        String hostipal_name_type01 = "";
        String docotor_name_type01 = "";
        if(!TextUtils.isEmpty(mBankBean.getFdept())){
            hostipal_name_type01 = mBankBean.getNick()+"-"+mBankBean.getFdept();
           // docotor_name_type01 = mBankBean.getFdept()+"-"+"推荐医生";
            docotor_name_type01 = mBankBean.getFdept();
        }else {
            hostipal_name_type01=""+mBankBean.getNick();
           docotor_name_type01 = "推荐医生";
        }
        tv_hostipal_name_type01.setText(hostipal_name_type01);


        tv_docotor_name_type01.setText(docotor_name_type01);

        //擅长
        String detail = "<b>简介: </b>"+ (TextUtils.isEmpty(mBankBean.getFdeptdes()) ? "暂无" : mBankBean.getFdeptdes());
        tv_detail01.setText(Html.fromHtml(detail));

        String hostipal_name_type02 = "";
        String docotor_name_type02 = "";
        if(!TextUtils.isEmpty(mBankBean.getFdept())){
            tv_look_more01.setVisibility(View.VISIBLE);
            hostipal_name_type02 = mBankBean.getNick();
            docotor_name_type02 = mBankBean.getFdept();
           docotor_name_type02 =  "推荐医生";
        }else {
            tv_look_more01.setVisibility(View.GONE);
            hostipal_name_type02=""+mBankBean.getNick();
            docotor_name_type02 = "推荐医生";
        }
        tv_hostipal_name_type02.setText(hostipal_name_type02);


        tv_docotor_name_type02.setText(docotor_name_type02);
        //擅长
        String detail2 = "<b>简介: </b>"+ (TextUtils.isEmpty(mBankBean.getGoodremedy()) ? "暂无" : mBankBean.getGoodremedy());
        tv_detail02.setText(Html.fromHtml(detail2));

        if ("y".equals(mBankBean.getIsfollow())) {
            tvFollowState.setText("取消关注");
        } else {
            tvFollowState.setText("点击关注");
        }
        if(TextUtils.isEmpty(mBankBean.getFdept())){
            tv_look_more01.setVisibility(View.GONE);
            View view = View.inflate(MyHealthBankDetailActivity.this,R.layout.textview_no_data,null);
            TextView textView = (TextView) view.findViewById(R.id.tv_text_nodate);
            textView.setText("暂无推荐的医生");
            ll_doctors01.removeAllViews();
            ll_doctors01.setGravity(Gravity.CENTER);
            ll_doctors01.addView(view);
        }else{
            provider.getAttentionBankDoctorList("1", "10", doctorId,mBankBean.getFdept() ,GET_ATTENTION_BANK_DOCTOR_LIST);
        }

        provider.getAttentionBankDoctorList("1", "10", doctorId,"", GET_ATTENTION_BANK_DOCTOR_LIST_All);

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_follow_state:
                if ("y".equals(mBankBean.getIsfollow())) {
                    //已经关注了就取消关注
                    provider.delAttentionBank(mBankBean.getDoctorid(), DEL_ATTENTION_BANK);
                } else {
                    provider.addAttentionBank(mBankBean.getDoctorid(), ADD_ATTENTION_BANK);
                }
                break;
            case R.id.ll_show_more:
              /*  if ("收起详情".equals(tvShowMore.getText())){
                    tvShowMore.setText("查看详情");
                    ivShowMore.setBackgroundResource(R.drawable.home_docinfo_more_up);
                    tvBankGoodness.setText(ViewUtil.getSubString(tvBankGoodness, mBankBean.getGoodremedy(), 0, this));
                    tvBankIntroduction.setText(ViewUtil.getSubString(tvBankIntroduction, mBankBean.getExperience(), 0, this));
                }else{
                    tvShowMore.setText("收起详情");
                    ivShowMore.setBackgroundResource(R.drawable.home_docinfo_more_down);
                    tvBankGoodness.setText(ViewUtil.getSubString(tvBankGoodness, mBankBean.getGoodremedy(), 5, this));
                    tvBankIntroduction.setText(ViewUtil.getSubString(tvBankIntroduction, mBankBean.getExperience(), 5, this));
                }*/
                break;
           case R.id.tv_look_more01:
                Bundle docDetailBundle = new Bundle();
                docDetailBundle.putString(BanklPlaceDoctorListActivity.DOCTOR_ID,mBankBean.getDoctorid());
                docDetailBundle.putString(BanklPlaceDoctorListActivity.DOCTOR_NAME,mBankBean.getNick());
                docDetailBundle.putString(BanklPlaceDoctorListActivity.DOCTOR_DEPARTMENT,mBankBean.getFdept());
                ActivityUtils.showActivity(this, BanklPlaceDoctorListActivity.class, docDetailBundle);
                break;
            case R.id.tv_look_more02:
                Bundle docDetailBundle2 = new Bundle();
                docDetailBundle2.putString(BanklPlaceDoctorListActivity.DOCTOR_ID,mBankBean.getDoctorid());
                docDetailBundle2.putString(BanklPlaceDoctorListActivity.DOCTOR_NAME,mBankBean.getNick());
                docDetailBundle2.putString(BanklPlaceDoctorListActivity.DOCTOR_DEPARTMENT,"");
                ActivityUtils.showActivity(this, BanklPlaceDoctorListActivity.class, docDetailBundle2);
                break;
            default:
                break;
        }
    }
}
